package com.example.friend.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.example.core.constants.Constants;
import com.example.core.entity.PageDataInfo;
import com.example.core.utils.ThreadLocalUtil;
import com.example.friend.component.manager.ExamCacheManager;
import com.example.friend.entity.DO.ExamDO;
import com.example.friend.entity.DTO.ExamQueryDTO;
import com.example.friend.entity.VO.ExamVO;
import com.example.friend.mapper.ExamMapper;
import com.example.friend.service.IExamService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ExamServiceImpl implements IExamService {
    @Autowired
    private ExamMapper examMapper;
    @Autowired
    private ExamCacheManager examCacheManager;

    @Override
    public PageDataInfo<ExamVO> list(ExamQueryDTO examQueryDTO) {
        // 查询redis中是否有数据
        // todo 不支持模糊查询
        Long total = examCacheManager.getListSize(examQueryDTO.getType(), null);
        List<ExamVO> examVOList;
        if (total == null || total <= 0) {
            // 没有则查询数据库
            PageHelper.startPage(examQueryDTO.getPageNum(), examQueryDTO.getPageSize());
            examVOList = examMapper.selectExamList(examQueryDTO);
            examCacheManager.refreshCache(examQueryDTO.getType(), null);
            total = new PageInfo<>(examVOList).getTotal();
        } else {
            // 有则查询redis
            examVOList = examCacheManager.getExamVOList(examQueryDTO, null);
            total = examCacheManager.getListSize(examQueryDTO.getType(), null);
        }
        if (CollectionUtil.isEmpty(examVOList)) {
            return PageDataInfo.empty();
        }
        assembleExamVOList(examVOList);
        return PageDataInfo.success(examVOList, total);
    }

    @Override
    public ExamDO getExam(Long examId) {
        return examMapper.selectById(examId);
    }


    @Override
    public String getFirstQuestion(Long examId) {
        checkAndRefresh(examId);
        return examCacheManager.getFirstQuestion(examId).toString();
    }

    @Override
    public String preQuestion(Long examId, Long questionId) {
        checkAndRefresh(examId);
        return examCacheManager.preQuestion(examId, questionId).toString();
    }

    @Override
    public String nextQuestion(Long examId, Long questionId) {
        checkAndRefresh(examId);
        return examCacheManager.nextQuestion(examId, questionId).toString();
    }



    /**
     * 从给定竞赛中选出已报名的竞赛
     * @param examVOList 给定竞赛
     */
    private void assembleExamVOList(List<ExamVO> examVOList) {
        Long userId = ThreadLocalUtil.get(Constants.USER_ID, Long.class);
        List<Long> userExamIdList = examCacheManager.getAllUserExamList(userId);
        if (CollectionUtil.isEmpty(userExamIdList)) {
            return;
        }
        for (ExamVO examVO : examVOList) {
            if (userExamIdList.contains(examVO.getExamId())) {
                examVO.setEnter(true);
            }
        }
    }

    private void checkAndRefresh(Long examId) {
        Long listSize = examCacheManager.getExamQuestionListSize(examId);
        if (listSize == null || listSize <= 0) {
            examCacheManager.refreshExamQuestionCache(examId);
        }
    }
}
